package com.bhs.sansonglogistics.ui.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.ArriveBean;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

public class AddLineAdapter extends RecyclerView.Adapter<AddLineAdapter.ViewHolder> {

    private List<ArriveBean> list;
    private int logistics_id;

    public AddLineAdapter(List<ArriveBean> list, int logistics_id) {
        this.list = list;
        this.logistics_id = logistics_id;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_arrival_information, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.setIsRecyclable(false);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ArriveBean item = list.get(position);
        holder.et_company.setText(item.getOutlets_name());
        holder.et_manager.setText(item.getOutlets_contact());
        holder.et_phone.setText(item.getOutlets_mobile());
        holder.et_address.setText(item.getOutlets_address());
        TextWatcher twCompany = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setModification(true);
                if (TextUtils.isEmpty(s.toString())) {
                    item.setOutlets_name("");
                } else {
                    item.setOutlets_name(s.toString());
                }
                myClickListener.onClick(null);
            }
        };
        holder.et_company.setTag(twCompany);
        holder.et_company.addTextChangedListener(twCompany);

        holder.et_manager.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setModification(true);
                if (TextUtils.isEmpty(s.toString())) {
                    item.setOutlets_contact("");
                } else {
                    item.setOutlets_contact(s.toString());
                }
                myClickListener.onClick(null);
            }
        });

        holder.et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setModification(true);
                if (TextUtils.isEmpty(s.toString())) {
                    item.setOutlets_mobile("");
                } else {
                    item.setOutlets_mobile(s.toString());
                }
                myClickListener.onClick(null);
            }
        });
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemChildClick(view, position);
            }
        });
        holder.iv_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemChildClick(view, position);
            }
        });


        holder.et_company.setFocusableInTouchMode(logistics_id == item.getLogistics_id()||item.getLogistics_id()==0);
        holder.et_manager.setFocusableInTouchMode(logistics_id == item.getLogistics_id()||item.getLogistics_id()==0);
        holder.et_phone.setFocusableInTouchMode(logistics_id == item.getLogistics_id()||item.getLogistics_id()==0);
        holder.iv_location.setEnabled(logistics_id == item.getLogistics_id()||item.getLogistics_id()==0);
        holder.iv_delete.setVisibility(logistics_id == item.getLogistics_id()||item.getLogistics_id()==0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_delete;
        private FrameLayout iv_location;
        private EditText et_company, et_manager, et_phone;
        private TextView et_address;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            iv_location = itemView.findViewById(R.id.iv_location);
            et_company = itemView.findViewById(R.id.et_company);
            et_manager = itemView.findViewById(R.id.et_manager);
            et_phone = itemView.findViewById(R.id.et_phone);
            et_address = itemView.findViewById(R.id.et_address);
        }
    }

    public void setOnItemChildClickListener(OnItemChildClickListener listener) {
        this.listener = listener;
    }

    private OnItemChildClickListener listener;

    public interface OnItemChildClickListener {
        void onItemChildClick(View view, int position);
    }

    private MyClickListener myClickListener;

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.myClickListener = l;
    }
}
